package sioserver;

/**
 * Dispatcher拿到资源后，负责创建新的任务处理程序，这里是直接创建，在实际应用中，任务处理程序可能会被放到线程池中去执行。
 * 
 * @author Xinz
 *
 */
public class SIODispatcher {

	private Integer requestId;
	private Integer resourceId;
	private SIODemultiPlexer demultiPlexer;
	
	public SIODispatcher(SIODemultiPlexer demultiPlexer, Integer requestId, Integer resourceId) {
		super();
		this.requestId = requestId;
		this.resourceId = resourceId;
		this.demultiPlexer = demultiPlexer;
	}
	
	/**
	 * 当分发器有资源时请求Handler处理
	 * @return
	 */
	public SIORequestHandler createRequestHandler() {
		return new SIORequestHandler(this, requestId, resourceId);
	}
	
	/**
	 * 当Handler处理完后释放资源
	 * @param i
	 */
	public synchronized void freeResource(Integer i) {
		demultiPlexer.returnResources(i);
	}
}
